window.onload=function(){
	var oli=document.getElementsByTagName("li");
	var odiv=document.getElementById("wrap").getElementsByTagName("div");
	for(var i=0;i<oli.length;i++){
		oli[i].setAttribute("idx",i);
		oli[i].onclick=function(){
			for(var j=0;j<oli.length;j++){
				oli[j].removeAttribute("class");
				odiv[j].removeAttribute("class");	
			}
			this.className='liactive';
			odiv[this.getAttribute("idx")].className="dactive";
		}
	}
	setTimer();
	var otable=document.getElementById("otable");
	var tr=otable.rows;  //得到表格中的行，tr是一个数组
	var chkipt=document.getElementsByClassName("chkipt");
//删除、增加数量、减少数量
	for(var i=1;i<tr.length-1;i++){  
		//由于HTML设置，获取的是整个table中的tr元素，去掉第1行和最后一行
		tr[i].onclick=function(){
			var el=window.event.srcElement;
			var reduce=this.getElementsByTagName("span")[1];
			var add=this.getElementsByTagName("span")[2];
			var ipt=this.getElementsByTagName("input")[1];
			var odel=this.getElementsByTagName("span")[3];
			var ivalue=parseInt(ipt.value);
			if(el==reduce){
				if(ivalue>0)
				ipt.value=ivalue-1;
			}
			else if(el==add){
				ipt.value=ivalue+1;
			}
			else if(el==odel){
				this.parentElement.removeChild(this);
			}
			else{
				return;}
			osum(this);
			oTotal();
		}
//手动输入时读取其输入值并计算	
	tr[i].getElementsByTagName("input")[1].onkeyup =function(){
		var val = this.value;
   		var otr = this.parentElement.parentElement;
   		if (isNaN(val) || val < 0 ) {
   			 val = 1;
   		}
	    this.value = val;
		osum(otr);
		}
	}
//选择及全选判定及计算
	for(var x=0;x<chkipt.length;x++){
		chkipt[x].onclick=function(){
			if(this.id=="sAll"){
				for(var m=0;m<chkipt.length;m++){
					chkipt[m].checked=this.checked;
				}
			}
			oTotal();
		}
	}
	function setTimer(){
		var i=10;
		var op=document.getElementById("op");
		var timer=setInterval(function(){
		if(i){
			op.innerHTML=i;
			i--;
		}
		else
		{
			clearInterval(timer);
			op.innerHTML="2022114057钟华";
			i=10
		}
	},1000);
}
//计算总价及总数量
	function oTotal(){
		var selspan=document.getElementsByClassName("sel");
		var onum=0;
		var oprice=0;
		for(var z=1;z<tr.length-1;z++){
			if(tr[z].getElementsByTagName("input")[0].checked){
				onum+=parseInt(tr[z].getElementsByTagName("input")[1].value) ;
				oprice+=parseFloat(tr[z].cells[4].innerHTML); 
			}
		}
	selspan[0].innerHTML=onum;
	selspan[1].innerHTML=oprice.toFixed(2);
}	
//计算小计
	function osum(a){
		var tds=a.cells;
		var price=parseFloat(tds[2].innerHTML);
		var ipt=a.getElementsByTagName("input")[1];
		tds[4].innerHTML=(parseFloat(price*ipt.value)).toFixed(2);
	}

}